<template>
  <div class='m-attribute'>
    <div style='padding: 0 0 20px;'>
      <el-form :inline='true'>
        <el-form-item label='属性类别：'>
          <a-select v-model='searchType' :options='searchAttrList' />
        </el-form-item>
        <el-button type='primary' @click='search'>搜索</el-button>
        <el-button type='primary' @click='createAttr'>创建属性</el-button>
      </el-form>
    </div>
    <a-table :data='data' :cols='tableKeys' key='t1'>
      <el-table-column align="center" label="操作" width='240' slot='ahead'>
        <template slot-scope="scope">
          <div class='m-firmAttr-table-handle'>
            <el-button size='mini' @click='updateAttr(scope.row.id)'>修改</el-button>
            <el-button size='mini' @click='deleteAttr(scope.row.id)'>删除</el-button>
            <el-button size='mini' v-if='scope.row.value_type === "0"'  @click='toChild(scope.row.id, scope.row.name)'>分类</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
    <u-dialog ref='dialog' @close='closeDialog' :openType='openType' />
  </div>
</template>

<script>
import uDialog from '@/module/sys/attribute/dialog'
import { valueTypeList, attrTypeList } from '@/api/attribute'
import { mapMutations } from 'vuex'

export default {
  name: 'mAttribute',

  components: {
    uDialog
  },

  data() {
    const filterMust = (value) => {
      return value === '0' ? '否' : '是'
    }
    const filterValue = (value) => {
      const obj = valueTypeList.filter(v => v.value === value)
      return obj.length > 0 ? obj[0].label : value
    }
    const filterAttr = (value) => {
      const obj = attrTypeList.filter(v => v.value === value)
      return obj.length > 0 ? obj[0].label : value
    }
    return {
      tableKeys: [
        { prop: 'name', label: '属性名称', minWidth: '100' },
        { prop: 'type', label: '属性类别', minWidth: '100', filter: filterAttr },
        { prop: 'value_type', label: '值类型', minWidth: '100', filter: filterValue },
        { prop: 'is_query', label: '是否查询用', minWidth: '100', filter: filterMust },
        { prop: 'is_require', label: '必填', minWidth: '100', filter: filterMust },
        { prop: 'value', label: '下级分类', minWidth: '100' }
      ],
      data: [],
      ifRequesting: false,
      openType: 'add',
      searchType: '',
      searchAttrList: [{ label: '所有', value: '' }].concat(attrTypeList)
    }
  },

  mounted() {
    this.$refs.page.changePage()
  },

  methods: {
    ...mapMutations(['setTabRoutes']),

    routerPush(type, args) {
      // push 到缓存里
      this.setTabRoutes({ type: 'add', obj: { ...this.getRouterInfoAccordingName(type), ...args } })
    },

    async fetchData(aPageNum, aPageSize) {
      const params = {}
      if (this.searchType) {
        params.aType = this.searchType
      }
      return this.$api.utilsGetAttributes({
        aMerchantId: this.aFirmId,
        aPageNum,
        aPageSize,
        ...params
      })
    },

    search() {
      this.$refs.page.changePage()
    },

    getData(data) {
      this.data = data
    },

    createAttr() {
      this.openType = 'add'
      this.$refs.dialog.__show()
    },

    deleteAttr(aId) {
      if (this.ifRequesting) return false
      this.__messageBox({
        title: '你确认要删除该属性吗？',
        successCallback: async () => {
          this.ifRequesting = true
          const { code } = await this.$api.utilsDeleteAttr({ aId })
          this.ifRequesting = false
          this.$refs.page.changePage()
          return code
        }
      })
    },

    updateAttr(aId) {
      this.openType = 'modify'
      this.$refs.dialog.__show('modify', aId)
    },

    toChild(id, name) {
      this.routerPush('sysClasses', { query: { id, name } })
      this.$router.push({ name: 'sysClasses', query: { id, name } })
    },

    closeDialog(type) {
      if (type) {
        this.$refs.page.changePage()
      }
    }
  }
}
</script>

<style scoped>

</style>